/**
 * 定义公共指令
 */
(function(window, angular, undefined){

	var CommonDirective = angular.module('CommonDirective', ['ui.router','itfreer','waterCommon']);
			
	CommonDirective.filter('cut', function () {
		  return function (value, wordwise, max, tail) {
		    if (!value) return '';

		    max = parseInt(max, 10);
		    if (!max) return value;
		    if (value.length <= max) return value;

		    value = value.substr(0, max);
		    if (wordwise) {
		      var lastspace = value.lastIndexOf(' ');
		      if (lastspace != -1) {
		        value = value.substr(0, lastspace);
		      }
		    }

		    return value + (tail || ' …');
		  };
		});
	CommonDirective.directive("comSelecter", function($timeout) {
		  return { 
			require : "ngModel",
		    restrict: "E",        // 指令是一个元素 (并非属性) 
		    scope: {              // 设置指令对于的scope 
		      id: "@",          // id 值传递 （字符串，单向绑定）
		      model:"=ngModel",
		      mulit:"=ngMulit",
		      data: "=", //双向绑定
		      change:"&ngChange",
		      singleKey:"=singleKey",//互斥key，选中了这个key其他的就自动清空
		      more:"=ngMore" //大于几个就显示更多按钮
		    }, 
		    template:             // 替换HTML (使用scope中的变量)
		      '<div >'+
		      '<ul class="zs-data-sizer" id={{id}} style="float:left;">'+
		      ' <li ng-show="!needMore || show || ($index <= more)" repeat-finish ng-finish="repeatFinish()" ng-repeat="item in data" ng-click="uiClick(item.k)" ng-class="isChecked(item.k)?\'selected\':\'\'"><a href="#" >{{item.v}}</a></li>'+
		      '</ul><a ng-click="moreClick()" ng-show="data.length>more" href="#" style="color: #908D8D;text-decoration: underline;float:left;padding: 3px 0px;">{{moreText}}</a></div>', 
		    replace: true,        // 使用模板替换原始标记 
		    transclude: false,    // 不复制原始HTML内容 
		    link: function ($scope, element, attrs, controller) {
		    	$scope.selectModels = [];
		    	$scope.show = false;
		    	$scope.moreText = "展开>>";
		    	$scope.singleKey = attrs.singleKey;
		    	$scope.controller = controller;
		    	$scope.modelFlag = attrs.tag;
		    	
		    	$scope.moreClick = function(){
		    		$scope.show=!$scope.show;
		    		if($scope.show){
		    			$scope.moreText = "收起<<";
		    		}
		    		else{
		    			$scope.moreText = "展开>>";
		    		}
		    	}
		    	
		    	if($scope.more){
		    		$scope.needMore = true;//打开更多的控制
		    	}
		    	
		    	/**
		    	 * 判断子项是否选中
		    	 */
		    	$scope.isChecked = function(k){
		    		
		    		if($scope.model == undefined){
		    			return false;
		    		}
		    		
		    		if($scope.model instanceof Array){
		    			if($scope.model.indexOf(k) > -1){
			    			return true;
			    		}
			    		else{
			    			return false;
			    		}
		    		}
		    		else{
		    			return $scope.model == k;
		    		}
		    	}
		    	
		    	/**
		    	 * 渲染完成事件,当数据重新加载后，判断上一次选中的项是否存在于新的数据当中，如果存在的话，则仍然保持选中，否则默认选中第一个
		    	 */
		    	$scope.repeatFinish = function(){
		    		var change = false;
		    		var containKey = [];
		    		
		    		for(var i=0;i<$scope.data.length;i++){
		    			var k = $scope.data[i].k;
		    			
		    			if($scope.mulit){
		    				for(var j=0;j<$scope.selectModels.length;j++){
		    					if(k === $scope.selectModels[j]){
		    						containKey.push(k);
		    					}
		    				}
		    			}
		    			else{
		    				if(k === $scope.selectModels){
	    						containKey.push(k);
	    					}
		    			}
		    		}
		    		
		    		if(containKey.length > 0){
		    			if($scope.mulit){
		    				if(containKey.length != $scope.model.length){
		    					change = true;
		    				}
			    			$scope.model = angular.copy(containKey);
			    			$scope.selectModels = angular.copy(containKey);
			    		}
			    		else{
			    			$scope.model = angular.copy(containKey[0]);
			    			$scope.selectModels = [angular.copy(containKey[0])];
			    		}
		    		}
		    		else{
		    			change = true;
		    			$scope.selectModels = [angular.copy($scope.data[0].k)];
		    			if($scope.mulit){
			    			$scope.model = [angular.copy($scope.data[0].k)];
			    			
			    		}
			    		else{
			    			$scope.model = angular.copy($scope.data[0].k);
			    		}
		    		}
		    		
		    		if(change && $scope.change()){
		    			if($scope.modelFlag == "curPollStationName"){
		    				var i=0;
		    			}
		    			$scope.controller.$setViewValue($scope.model);
		    			$timeout(function(){$scope.change()($scope.model)},50);
		    		}
		    	}
		    	
		    	/**
		    	 * 选择事件
		    	 */
		    	$scope.uiClick = function(k){
		    		if($scope.selectModels.length == 0 && $scope.model != null){
		    			$scope.selectModels = $scope.selectModels.concat($scope.model);
		    		}
		    		//如果当前点击的key已经被选中，并且选中的不是互斥key，那么就把它置为取消选中状态
		    		if($scope.selectModels.indexOf(k) > -1 && $scope.singleKey != k && $scope.mulit && ($scope.selectModels.length >= 2)){
		    			$scope.selectModels.remove(k);
		    		}
		    		else if(k != $scope.model){
		    			//判断是否选中的是互斥key，如果是的话，先清空，然后加入，否则走常规逻辑
		    			if(k === $scope.singleKey){
		    				$scope.selectModels = [];
		    				$scope.selectModels.push(k);
		    			}
		    			else{
		    				$scope.selectModels.remove($scope.singleKey);
		    				if($scope.mulit){
			    				$scope.selectModels.push(k);
			    			}
			    			else{
			    				$scope.selectModels = [];
			    				$scope.selectModels.push(k);
			    			}
		    			}
		    		}
		    		
		    		if(!$scope.mulit){
		    			$scope.model = angular.copy($scope.selectModels[0]);
		    		}
		    		else{
		    			$scope.model = angular.copy($scope.selectModels);
		    		}
		    		
		    		if($scope.change()){
		    			$timeout(function(){$scope.change()($scope.model)},50);
		    		}
		    	}
		    }
		  } 
		});
	
	CommonDirective.directive("companySelecter", function($timeout,$waterDataCommon) { 
		  return { 
			transclude: false,    // 不复制原始HTML内容 
		    restrict: "E",        // 指令是一个元素 (并非属性) 
		    require : "ngModel",
		    replace: true,        // 使用模板替换原始标记 
		    scope: {              // 设置指令对于的scope 
		      id: "@",          // id 值传递 （字符串，单向绑定）
		      pollType:"@",
		      model:"=ngModel",
		      data: "=",
		      change:"&ngChange"
		    }, 
		    template:             // 替换HTML (使用scope中的变量) 
		    	"<div id=\"areaForPipeSearch\" class=\"zs-area\" style=\"text-align: left;\">\n" +
		    	" <a href=\"\" class=\"area zs-ddl\">\n" + 
		    	"  <span id=\"choiceName\">{{model.companyName}}</span>\n" + 
		    	" </a>\n" + 
		    	" <div class=\"zs-ddl-contain\" style=\"position: fixed;overflow: auto;\">\n" + 
		    	"  <div class=\"selected\">\n" + 
		    	"      <i class=\"close\">×</i>\n" + 
		    	"    </div>\n" + 
		    	"    <div class=\"list\" style=\"overflow: visible;\">\n" + 
		    	"      <ul>\n" + 
		    	"        <li class=\"fir-list\" ng-click=\"changeCompany({SDEEXP_NO:'',SDENAME:'全部'})\">\n" + 
		    	"          <span class=\"close\"><a href=\"#\" areaCode='all'>全部</a></span></li>\n" + 
		    	"      </ul>\n" + 
		    	"      <ul>\n" + 
		    	"        <li class=\"sec-list\">\n" + 
		    	"          <ul>\n" + 
		    	"            <li ng-repeat=\"curRow in companyList\" ng-click=\"changeCompany(curRow)\"><span class=\"close\">\n" + 
		    	"              <a href=\"#\" title=\"{{curRow.SDENAME}}({{curRow.CPMPANYCOUNT}})\">{{curRow.SDENAME | cut:true:6:' ...'}}({{curRow.CPMPANYCOUNT}})</a></span></li>\n"+
		    	"          </ul>\n" + 
		    	"        </li>\n" + 
		    	"      </ul>\n" + 
		    	"    </div>\n" + 
		    	"  </div>\n" + 
		    	"</div>", 
		    link: function ($scope, element, attrs, controller) {
		    	if($scope.model == null){
	    			$scope.model = {companyName:"全部"};
	    		}
		    	$waterDataCommon.getPageRows("/CompanyInfo/companyStationCountList.do","companyList",{"poll_type":$scope.pollType},100,1,function(d){
					if(d){
						$scope.companyList = d.data;
						$("a.zs-ddl").click(function() {
							$(this).next(".zs-ddl-contain").show("fast");
						});
						$(".zs-ddl-contain").find(".close").click(function(){
							$(this).parents(".zs-ddl-contain").hide("fast");
						})
						$(".zs-ddl-contain .list").niceScroll();
					}
				})
				
				$scope.changeCompany = function(company){
		    		$(".zs-ddl-contain").hide("fast");
		    		$scope.model.companyName = company.SDENAME;
		    		$scope.model.companyCode = company.SDEEXP_NO;
		    		if($scope.change()){
		    			$scope.change()($scope.model);
		    		}
		    	}
		    }
		  } 
		});
	
	CommonDirective.directive("comCombobox", function($timeout,$waterDataCommon,$parse) {
		  return { 
			transclude: false,    // 不复制原始HTML内容 
		    restrict: "E",        // 指令是一个元素 (并非属性) 
		    require : "ngModel",
		    replace: true,        // 使用模板替换原始标记 
		    scope: {              // 设置指令对于的scope 
		      id: "@",          // id 值传递 （字符串，单向绑定）
		      pollType:"@",
		      width:"@width",
		      model:"=ngModel",
		      data: "=",
		      change:"&ngChange"
		    }, 
		    template:             // 替换HTML (使用scope中的变量) 
		    	'<div class="lin-combobox">'+
				'   <div class="selectText" ng-style="{width:width}" ng-click="labelClick()">'+
		    	' 		<span ng-bind="selectText"></span>'+
		    	'  </div>'+
				'   <ul class="zs-data-sizer" ng-mouseleave="ulShow = false" ng-style="{width:width}" ng-show="ulShow" > '+
				'     <li ng-class="model==curRow.id?\'select\':\'\'" repeat-finish ng-repeat="curRow in data" ng-finish="repeatFinish()" ng-click="itemSelect(curRow)" ><a href="#">{{curRow.text}}</a></li>'+
				'   </ul>'+
				'</div> 	',
		    link: function ($scope, element, attrs, controller) {
		    	$scope.ulShow = false;
		    	$scope.selectItem = null;
		    	$scope.labelClick = function(){
		    		$scope.ulShow = !$scope.ulShow;
		    	}
		    	
		    	$scope.itemSelect = function(item){
		    		$scope.selectText = item.text;
		    		$scope.model = item.id;
		    		$scope.selectItem = item;
		    		$scope.ulShow = false;
		    		if($scope.change()){
		    			$scope.change()(item);
		    		}
		    	}
		    	
		    	$scope.ulMouseOut = function(){
		    		$scope.ulShow = false;
		    	}
		    	
		    	controller.$render = function(){
		    		var viewValue = controller.$viewValue;
		    		$scope.model = viewValue;
		    		$scope.repeatFinish();
		    	}
		    	
		    	$scope.repeatFinish = function(){
		    		var bingo = false;
		    		
		    		if($scope.data.length > 0){
		    			for(var i=0;i<$scope.data.length;i++){
			    			var item = $scope.data[i];
			    			if(item.id == $scope.model){
			    				$scope.selectText = item.text;
			    				$scope.selectItem = item;
			    				bingo = true;
			    				break;
			    			}
			    		}
			    		if(!bingo){
			    			$scope.selectText = "请选择";
			    			$scope.model = "";
			    		}
		    		}
		    		else{
		    			$scope.selectText = "请选择";
		    			$scope.model = "";
		    		}
		    		
	    			if($scope.change()){
	    				if(bingo){
	    					$scope.change()($scope.selectItem);
	    				}
	    				else{
	    					$scope.change()(false);
	    				}
		    		}
		    	}
		    }
		  } 
		});
	
	
	CommonDirective.directive("stationList", function($timeout,$rootScope,$wdp) { 
		  return {
			transclude: false,    // 不复制原始HTML内容 
		    restrict: "A",        // 指令是一个元素 (并非属性) 
		    require : "ngModel",
		    replace: true,        // 使用模板替换原始标记 
		    scope: {              // 设置指令对于的scope 
		      height:"@",
		      model:"=ngModel",
		      pollType:"@",
		      change:"&ngChange",
		      isReport:"@",
		      isCompany:"@",
		      companySelecterText:"@"
		    }, 
		    template:function(){
		    	var userid="";
		    	
		    	try {
		    		userid = $rootScope.currentUser.C_USERNAME;
				} catch (e) {
					// TODO: handle exception
				}
		    	
		    	console.log(userid);
		    	
		    	var stateHtml = "<tr >\n" + 
		    	"            <td colspan=\"2\" class=\"left tip\">\n" + 
		    	"              <span ng-click=\"changeStationStatus(0)\" class=\"gsstation_zc0 text\" ng-if='waterType ==3' ng-class=\"queryObj.stationStatus=='0'?'selected':''\">正常({{statusStatistics.staNormal}})</span>\n" + 
		    	"   		   <span ng-click=\"changeStationStatus(0)\" class=\"station_zc0 text\" ng-if='waterType !=3' ng-class=\"queryObj.stationStatus=='0'?'selected':''\">正常({{statusStatistics.staNormal}})</span>\n" + 
		    	"              <span ng-click=\"changeStationStatus(1)\" class=\"station_cb1 text\" ng-class=\"queryObj.stationStatus=='1'?'selected':''\">超标({{statusStatistics.staOverproof}})</span>\n" + 
		    	"              <span ng-click=\"changeStationStatus(2)\" class=\"station_yc2 text\" ng-class=\"queryObj.stationStatus=='2'?'selected':''\">异常({{statusStatistics.staExecption}})</span>\n" + 
		    	"              <span ng-click=\"changeStationStatus(3)\" class=\"station_gz3 text\" ng-class=\"queryObj.stationStatus=='3'?'selected':''\">故障({{statusStatistics.staFault}})</span>\n" + 
		    	"            </td>\n" + 
		    	"          </tr>\n" ;
		    	
		    	if(userid == "aomen"){
		    		stateHtml = "";
		    	}
		    	
		    	var html = "<div>\n"+
		    	"<div class=\"zs-form-tabs\">\n" +
		    	"      <table class=\"zs-form-tab\">\n" + 
		    	"        <tbody>\n" + 
		    	"          <tr ng-if='isCompany==\"true\"'>\n" + 
		    	"            <td class=\"right top\" width=\"25%\">{{companySelecterText}}：</td>\n" + 
		    	"            <td>\n" + 
		    	"              <company-selecter poll_type='{{pollType}}' ng-model=\"companySelected\" ng-change=\"changeCompany\"></company-selecter>\n" + 
		    	"            </td>\n" + 
		    	"          </tr>\n" + 
		    	"          <tr>\n" + 
		    	"		   <tr ng-if='waterType == 2 || waterType == 4'>"+
				"			 <td class='right top' width='25%'>所属政区：</td>"+
				"			 <td>"+
				"				<com-single-check data='area' ng-model='queryObj.DISTRICT' ng-change='queryClick'></com-single-check>"+
				"			 </td>"+
				"			</tr>"+
		    	"            <td class=\"right\">站点名称：</td>\n" + 
		    	"            <td colspan=\"2\"><input class=\"zs-input\" type=\"text\"\n" + 
		    	"              style=\"width: 145px;\" ng-model='queryObj.STATION_NAME'> <input\n" + 
		    	"              type=\"button\" name=\"ok\" ng-click=\"queryClick();\" value=\"查询\"\n" + 
		    	"              class=\"zs-btn\"></td>\n" + 
		    	"          </tr>\n" + 
		    	"<tr ng-if='isReport==\"true\"'>\n" +
		    	"     <td class=\"right\">监测项：</td>\n" + 
		    	"     <td style=\"width:224px\">\n" + 
		    	"       <div><input href=\"#\" class=\"zs-btn\" ng-show=\"!parmListShow\" ng-click=\"parmListShow = true\" value=\"展开监测项\"/></div>\n" + 
		    	"       <div id=\"parm_list\" ng-show=\"parmListShow\">\n" + 
		    	"         <label repeat-finish ng-finish=\"pollLoadFinish()\" class=\"report_poll_list\" ng-repeat=\"curRow in pollList\">\n" + 
		    	"           <input name=\"parmToCheck\" checked ng-click=\"pollLoadFinish()\" unit=\"{{curRow.UNIT}}\" couunit=\"{{curRow.COU_UNIT}}\" value=\"{{curRow.CODE}}\" parmText=\"{{curRow.NAME}}\" class=\"zs-btn\" type=\"checkbox\" style=\"padding-right:2px\"/>\n" + 
		    	"           {{curRow.NAME}}\n" + 
		    	"         </label>\n" + 
		    	"       </div>\n" + 
		    	"       <div style=\"clear: both;\"><input type=\"button\" class=\"zs-btn\" href=\"#\" ng-show=\"parmListShow\" value=\"全不选\" ng-click=\"unSelectParms()\"/></div>\n" + 
		    	"       <div style=\"clear: both;\"><input type=\"button\" class=\"zs-btn\" href=\"#\" ng-show=\"parmListShow\" value=\"收&nbsp;&nbsp;&nbsp;起\" ng-click=\"parmListShow = false\"/></div>\n" + 
		    	"     </td>\n" + 
		    	" </tr>"+
		    	stateHtml +
		    	"<tr  ng-if='waterType == 2 || waterType == 4' >\n" +
		    	" <td class=\"right\">数据来源：</td>\n" + 
		    	" <td colspan=\"2\" class=\"left\" >\n" + 
		    	"   <div class=\"tip\" >\n" + 
		    	"     <span class=\"station_tb4 text\">&nbsp;同步</span>\n" + 
		    	"     <span class=\"station_ftb5 text\">&nbsp;实时传输</span>\n" + 
		    	"   </div>\n" + 
		    	" </t>" +
		    	"        </tbody>\n" + 
		    	"      </table>\n" + 
		    	"    </div>"+
		    	"<div class=\"zs-data-list\"  ng-style=\"{height:height}\" style=\"overflow: auto;\">\n" +
		    	"      <div ng-repeat=\"curRow in stationList\" >\n" + 
		    	"		<div class=\"list\" ng-if=\"curRow.COMPANY_NAME_HAS_PARENT == null && curRow.COMPANY_NAME != null\" ng-click='companyClick(curRow.WWTP_ID)' guid='{{curRow.WWTP_ID}}' style=\"background-color: #e9e9e9;\">\n" +
		    	" 			<div style=\"float: left;cursor: pointer;width:200px;\">\n" + 
		    	"            <div style='font-size: 12px;' ng-style={'min-height':result.COMPANY_PHOTO?'45px':'20px'} >{{curRow.COMPANY_NAME}}</div>\n" + 
		    	"          </div>\n" + 
		    	"          <div style=\"float: right\">\n" + 
		    	"			<div class=\"img-wrap\">\n" +
		    	"              <a href=\"#\">\n" + 
		    	"                <img ng-if=\"result.COMPANY_PHOTO\" ng-src=\"{{hostBase}}{{result.COMPANY_PHOTO | firstImgUrl}}\"\n" + 
		    	"                  style=\"width: 50px; height: 40px;\">\n" + 
		    	"              </a>\n" + 
		    	"            </div>"+
		    	"          </div>\n" + 
		    	"        </div>" +
		    	"		<div ng-class=\"{list:true,selected:curRow.CODE==model.CODE}\"  ng-click = \"selectStation(curRow)\" company_guid='{{curRow.WWTP_ID}}'"+ 
		    	"				ng-style=\"{'padding-left':(curRow.COMPANY_NAME_HAS_PARENT == null && curRow.COMPANY_NAME == null?'0px':'15px')}\">"+
		    	"        <div  style=\"float: left;\" class=\"tip\">\n" + 
		    	"          <span ng-show='waterType == 3' ng-class=\"{gsstation_zc0:curRow.STATION_STATUS=='0',\n" + 
		    	"          			station_cb1:curRow.STATION_STATUS=='1',\n" + 
		    	"          			station_yc2:curRow.STATION_STATUS=='2',\n" + 
		    	"          			station_gz3:curRow.STATION_STATUS=='3'}\"></span>\n" + 
		    	"          <span ng-show='waterType != 3' ng-class=\"{station_zc0:curRow.STATION_STATUS=='0',\n" + 
		    	"          			station_cb1:curRow.STATION_STATUS=='1',\n" + 
		    	"          			station_yc2:curRow.STATION_STATUS=='2',\n" + 
		    	"          			station_gz3:curRow.STATION_STATUS=='3'}\"></span>\n" + 
		    	"		<span ng-if='waterType == 2 || waterType == 4' style=\"display:inline-block;\" ng-class=\"{station_tb4:curRow.IS_SYNCHRONIZED=='4',\n" +
		    	"       			 station_ftb5:curRow.IS_SYNCHRONIZED=='5'}\">\n" + 
		    	"		</span>"+
		    	"        </div>\n" + 
		    	"        <div style=\"float: left;cursor: pointer;width:170px;\">\n" + 
		    	"          <div style='font-size: 12px;'>{{curRow.NAME}}</div>\n" + 
		    	"		   <div> "+
				"				<div style='height: 10px;'></div>"+
				"				<a ng-show='curRow.CITY_NAME != null && curRow.AREA_NAME != null' style='cursor: pointer;' class='locate' >"+
				"					<span ng-show='curRow.CITY_NAME != null && curRow.AREA_NAME != null' ng-bind='curRow.CITY_NAME+\".\"+curRow.AREA_NAME'></span>"+
				"				</a>"+
				"			</div>"+
		    	"        </div>\n" + 
		    	"        <div style=\"float: right\">\n" + 
		    	"          <div class=\"img-wrap\">\n" + 
		    	"            <a href=\"#\">\n" + 
		    	"              <img ng-if=\"curRow['PHOTO']\"\n" + 
		    	"                ng-src=\"{{hostBase}}{{curRow['PHOTO'] | firstImgUrl}}\"\n" + 
		    	"                onclick=\"imgShow($(this))\"\n" + 
		    	"                style=\"width: 50px; height: 40px;\">\n" + 
		    	"              <img ng-if=\"!curRow['PHOTO']\"\n" + 
		    	"                ng-src=\"{{wdpjsRootPath}}wdp/images/zs-photo.png\"\n" + 
		    	"                style=\"width: 50px; height: 40px;\">\n" + 
		    	"              </a>\n" + 
		    	"          </div>\n" + 
		    	"        </div>\n" + 
		    	"      </div>\n" + 
		    	"	</div>"+
		    	"</div>"+
		    	"</div>";
		    	return html;
		    },
		    link: function ($scope, element, attrs, controller) {
		    	$wdp.stationListReady = false;
		    	$scope.queryObj = {};
		    	$scope.area={};
		    	$scope.waterType = getWaterType();
		    	$scope.hostBase = getHostBase();
		    	$scope.wdpjsRootPath = getJsBasePath();
		    	$scope.callNoticeParmChange = null;
		    	
		    	$scope.queryStation = function(){
		    		$scope.queryObj.BUILD_STATUS = '0';
		    		$waterDataCommon.post("/MonitorStation/getListWithCompany.do",$scope.queryObj).then(function(result){
		    			$scope.stationList = result.data;
		    			$wdp._cache_stationList = result.data;
		    			$scope.selectStation($scope.stationList[0]);
		    		});
		    		$waterDataCommon.post("/MonitorStation/getStatusStatistics.do",$scope.queryObj).then(function(result){
		    			$scope.statusStatistics = result;
		    			$wdp._cache_statusStatistics = result;
		    		});
		    		$waterDataCommon.post("/district/getDistrict.do").then(function(data){
						for(var i=0;i<data.qu.length;i++){
							data.qu[i].v=data.qu[i].NAME;
							data.qu[i].k=data.qu[i].CODE;
						}
						$scope.area = data.qu;
						$wdp._cache_area=data.qu;
		    		});
		    	}
		    	
		    	$scope.queryClick = function(){
		    		$scope.queryObj.stationStatus = "";
		    		$scope.queryStation();
		    	}
		    	/**
		    	 * 公司单击事件
		    	 */
		    	$scope.companyClick = function(companyGUID){
		    		var stataionDivs = $("div[company_guid='"+companyGUID+"']");
		    		stataionDivs.animate({height: 'toggle', opacity: 'toggle'}, "fast");
		    	}
		    	
		    	$scope.changeStationStatus = function(status){
					if($scope.queryObj.stationStatus == status){
						$scope.queryObj.stationStatus = "";
					}
					else{
						$scope.queryObj.stationStatus = status;
					}
					
					$scope.queryStation();
				}
		    	
		    	$scope.selectStation = function(station){
		    		$scope.model = station;
		    		$wdp._cache_station_selected_model = station;
		    		if(!$scope.model){
		    			return;
		    		}
		    		if($scope.isReport == "true"){
		    			//如果是报表模式，则再去获取站点的参数，参数获取到之后，再去通知参数变化了
		    			getStationPoll($scope.model.CODE);
		    		}
		    		else{
		    			//如果不是报表模式，则直接通知站点选择变化了
		    			noticeParmChange();
		    			$wdp.stationListReady = "true";
		    		}
		    		if($scope.change()){
		    			$scope.change()($scope.model);
		    		}
		    	}
		    	
		    	$scope.changeCompany = function(company){
					$scope.queryObj.WWTP_ID = company.companyCode;
					$scope.queryObj.companyName = company.companyName
					$scope.queryStation();
				}
		    	
		    	$scope.changeStationStatus = function(status){
					if($scope.queryObj.stationStatus == status){
						$scope.queryObj.stationStatus = "";
					}
					else{
						$scope.queryObj.stationStatus = status;
					}
					
					$scope.queryStation();
				}
		    	
		    	function getStationPoll(stationCode){
		    		$scope.pollList=[];
		    		if($scope.isReport == "true"){
		    			$waterDataCommon.getPageRows("/OnlineMonitor/getStationPollDict.do","stationPoll", {"STATION_ID":stationCode},100,1,function(data){
			    			$scope.pollList = data.data;
			    			if($scope.pollList == null || $scope.pollList.length < 1){
			    				noticeParmChange();
			    			}
						},null,null,false);
		    		}
		    	}
		    	
		    	//站点参数加载完成执行
				$scope.pollLoadFinish = function(){
					noticeParmChange();
					$wdp.stationListReady = "true";
				}
				
				/**
				 * 参数全不选
				 */
				$scope.unSelectParms = function(){
					$("input[name='parmToCheck']").removeAttr("checked");
				}
				
				/**
				 * 获取界面上勾选的参数
				 */
				function getReportParms(){
					if(!$scope.model){
						return;
					}
					var parmToCheckDom = $("input[name='parmToCheck']:checked");
					var parmToChecksStr = "";
					var parmToCheckList = [];
					
					for(var i=0;i<parmToCheckDom.length;i++){
						var parmCode = $(parmToCheckDom[i]).val();
						var parmText = "";
						var parmName = $(parmToCheckDom[i]).attr("parmText");
						var parmUnit = $(parmToCheckDom[i]).attr("UNIT");
						var parmCouUnit= $(parmToCheckDom[i]).attr("couunit");
						if(parmUnit){
							parmText = parmName + "(" + parmUnit + ")";
							parmCouText=parmName + "(" + parmCouUnit + ")";
						}
						else{
							parmText = parmName;
							parmCouText = parmName;
						}
						parmToChecksStr += $(parmToCheckDom[i]).val()+",";
						parmToCheckList.push({"name_original":parmCode,"name":parmCode.toUpperCase(),"text":parmText,parmName:parmName,"couunit":parmCouUnit,"unit":parmUnit});
					}
					$.cookie("parmToChecksStr",parmToChecksStr,{expires:7});
					var wwtpReportParm = {
								parmToChecksStr:parmToChecksStr,
								parmToCheckList:parmToCheckList,
								STATION_ID:$scope.model.CODE,
								STATION_NAME:$scope.model.NAME,
								MN:$scope.model.DEVICE_NUM
							};
					
					if($scope.pollList == null || $scope.pollList.length <1){
						wwtpReportParm.parmToChecksStr = "";
						wwtpReportParm.parmToCheckList = [];
					}
					
					return wwtpReportParm;
				}
		    	
		    	function noticeParmChange(){
		    		
		    		try {
		    			if($scope.callNoticeParmChange){
		    				clearTimeout($scope.callNoticeParmChange);
			    		}
					} catch (e) {
						console.log(e);
					}
		    		
					$scope.reportParms = getReportParms();
					$scope.$emit('dataPramChange', $scope.reportParms); 
				}
		    	
		    	//相应获取参数的事件
				$scope.$on('askedDataPramChange', function(event, data) {  
					noticeParmChange();
			    });
		    	
				if($wdp._cache_stationList){
					$scope.statusStatistics = $wdp._cache_statusStatistics;
					$scope.stationList = $wdp._cache_stationList;
					$scope.area=$wdp._cache_area;
		    		$scope.selectStation($wdp._cache_station_selected_model);
				}
				else{
					$scope.queryStation();
				}
				
				/**
				 * 这个的目的是，在多个状态，但是左边用的是同一个view和controller的情况下
				 * 在切换状态的时候，左边的controller不会再执行，会导致不会初始化
				 * 所以这里监听状态切换事件，在状态切换的时候，主动调一些方法
				 */
				$scope.$on("$stateChangeSuccess",function(){
					if($scope.waterType!='3'){
						$scope.callNoticeParmChange = setTimeout(function(){
							noticeParmChange();
						},500);
					}
				});
		    }
		  } 
		});

	
	CommonDirective.directive("repeatFinish", function($timeout) { 
		  return { 
		    restrict: "A",        
		    scope: {              // 设置指令对于的scope 
		      finish:"&ngFinish"
		    },
		    controller: [ "$scope", function ($scope) { 
		    	
		    }], 
		    link:function($scope, elm, attr){
              if($scope.$parent.$last){
              	if($scope.finish){
		    			$timeout($scope.finish,50);
		    		}
              }
          }
		  } 
		});

}(window, angular));